home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13034 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.3 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Settle a bet please
  5. Date: Wed, 03 Apr 96 23:39:20 GMT
  6. Organization: none
  7. Message-ID: <828574760snz@genesis.demon.co.uk>
  8. References: <4jfopb$o9n@news1.sympatico.ca> <4jh8rtINNosd@mayne.ugrad.cs.ubc.ca> <4jp8li$ue@eri2.erinet.com> <828542716snz@genesis.demon.co.uk> <4jufbc$erj@eri.erinet.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4jufbc$erj@eri.erinet.com> timb@erinet.com "Tim Berens" writes:
  15.  
  16. >>>{
  17. >>>    printf("size is: %d",sizeof("My Name"));
  18. >>>}
  19.  
  20. >>Remember sizeof doesn't produce a result of type int, rather size_t which
  21. >>is some implementation-dependent unsigned type.
  22.  
  23. >Do people participate in this newsgroup simply to show their smarts by
  24. >desperately seeking nits to pick?  
  25.  
  26. Not generally, as far as I can tell.
  27.  
  28. >Also, even though it is remotely possible that size_t is not the same
  29. >size as an int and that the compiler may not automatically cast it to
  30. >an int for the function call Or that the size of "My name" will exceed
  31. >32K and size_t will be a 2 byte unsigned integer,
  32.  
  33. size_t can't be an int so %d can never be the correct format specifier to
  34. use for it.
  35.  
  36. I've never come across a compiler which attempts to convert an argument in
  37. a variable argument list to an appropriate type. The language has very
  38. specific rules as to what a compiler should do with such an argument so
  39. the compiler can perform such a conversion if it can detect with certainty
  40. a case of undefined behaviour. That is possible in this case with a standard
  41. library function but having done so the much better approach would be to
  42. warn about the problem rather than bodge it. In fact a number of compilers
  43. do this. So one of the problems with the code is that it simply won't
  44. compile cleanly on some compilers (e.g. gcc with suitable options).
  45.  
  46. sizeof("My Name") is guaranteed to be 8 and representable as an int which is
  47. why I cast to int in this case. In the general case the correct solution
  48. is to use the %lu format specifier and cast to unsigned long.
  49.  
  50. > does it really matter in this example?
  51.  
  52. Yes, it is the difference between well written code that has a well defined
  53. meaning from the language spec. and low maintenance and poorly written
  54. code whose behaviour is undefined by the language and is a time bomb waiting
  55. to go off.
  56.  
  57. comp.lang.c should ideally be a newsgroup where you can learn something about
  58. the correct use of the C language. That means that bad usage should not go
  59. unchallenged.
  60.  
  61. > Can you name a compiler where
  62. >printf("size=%d",sizeof("My Name"));  will not properly display
  63. >"size=8"?
  64.  
  65. Not offhand but that is not really important. What is important is whether
  66. the code actually means anything or not. If you are building or porting large
  67. project you have to be able to trust its individual components.
  68.  
  69. However I could easily envisage a 64 bit system where size_t is a 64 bit
  70. unsigned long and int is 32 bit.  Possibly DOS compilers that implement a
  71. huge memory model may have size_t 32 bits and int 16 bits.
  72.  
  73. -- 
  74. -----------------------------------------
  75. Lawrence Kirby | fred@genesis.demon.co.uk
  76. Wilts, England | 70734.126@compuserve.com
  77. -----------------------------------------
  78.